package cslice

import (
	"strconv"
)

// 判断 值是否在一个slice中
func ValueInStringSlice(key string, slice []string) bool {
	for _, eachItem := range slice {
		if eachItem == key {
			return true
		}
	}
	return false
}

// 判断 值是否在一个 int64 型号  slice中
func ValueInInt64Slice(key int64, slice []int64) bool {
	for _, eachItem := range slice {
		if eachItem == key {
			return true
		}
	}
	return false
}

// @title StringToInt64
// @description []string 转换成 []int64
func StringToInt64(values []string) ([]int64, error) {
	if len(values) == 0 {
		return make([]int64, 0), nil
	}

	returnData := make([]int64, 0)
	for _, strV := range values {
		id, err := strconv.ParseInt(strV, 10, 64)
		if err != nil {
			return nil, err
		}
		returnData = append(returnData, id)
	}

	return returnData, nil
}

// @title StringSliceMainDiff
// @description []string 类型数组的main中有的　second没有的
func StringSliceMainDiff(main []string, second []string) []string {
	data := make([]string, 0)
	if len(main) == 0 {
		return data
	}

	if len(second) == 0 {
		return main
	}

	for _, str := range main {
		if !ValueInStringSlice(str, second) {
			data = append(data, str)
		}
	}

	return data
}

// @title Int64SliceMainDiff
// @description []int64 类型数组的main中有的　second没有的
func Int64SliceMainDiff(main []int64, second []int64) []int64 {
	data := make([]int64, 0)
	if len(main) == 0 {
		return data
	}

	if len(second) == 0 {
		return main
	}

	for _, str := range main {
		if !ValueInInt64Slice(str, second) {
			data = append(data, str)
		}
	}

	return data
}
